package com.anolesoft.yr.mbus.part.service.impl;

import com.anolesoft.yr.mbus.part.dto.PartSafetyQuantityDTO;
import com.anolesoft.yr.mbus.part.entity.PartSafetyQuantityDO;
import com.anolesoft.yr.mbus.part.mapper.PartSafetyQuantityMapper;
import com.anolesoft.yr.mbus.part.service.PartSafetyQuantityService;
import com.anolesoft.yr.util.CustomBeanUtils;
import com.anolesoft.yr.util.PageData;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author zhj
 * @date 2020/8/11 18:40
 */
@Service("partSafetyQuantityServiceImpl")
public class PartSafetyQuantityServiceImpl implements PartSafetyQuantityService {
    @Autowired
    private PartSafetyQuantityMapper partSafetyQuantityMapper;

    @Transactional
    @Override
    public int create(PartSafetyQuantityDTO partSafetyQuantityDTO) {
        return partSafetyQuantityMapper.insert(PartSafetyQuantityDO.builder()
                .objuid(partSafetyQuantityDTO.getObjuid())
                .stkMainUid(partSafetyQuantityDTO.getStkMainUid())
                .partModelUid(partSafetyQuantityDTO.getPartModelUid())
                .factoryCode(partSafetyQuantityDTO.getFactoryCode())
                .warningQuantity(partSafetyQuantityDTO.getWarningQuantity())
                .remark(partSafetyQuantityDTO.getRemark())
                .modifyUserUid(partSafetyQuantityDTO.getModifyUserUid())
                .modifyTime(partSafetyQuantityDTO.getModifyTime())
                .lowestReserve(partSafetyQuantityDTO.getLowestReserve())
                .highestReserve(partSafetyQuantityDTO.getHighestReserve())
                .deleteFlag(partSafetyQuantityDTO.getDeleteFlag())
                .createUserUid(partSafetyQuantityDTO.getCreateUserUid())
                .createTime(partSafetyQuantityDTO.getCreateTime())
                .build());
    }

    @Transactional
    @Override
    public int modify(PartSafetyQuantityDTO partSafetyQuantityDTO) {
        return partSafetyQuantityMapper.update(PartSafetyQuantityDO.builder()
                .objuid(partSafetyQuantityDTO.getObjuid())
                .stkMainUid(partSafetyQuantityDTO.getStkMainUid())
                .partModelUid(partSafetyQuantityDTO.getPartModelUid())
                .factoryCode(partSafetyQuantityDTO.getFactoryCode())
                .warningQuantity(partSafetyQuantityDTO.getWarningQuantity())
                .remark(partSafetyQuantityDTO.getRemark())
                .modifyUserUid(partSafetyQuantityDTO.getModifyUserUid())
                .modifyTime(partSafetyQuantityDTO.getModifyTime())
                .lowestReserve(partSafetyQuantityDTO.getLowestReserve())
                .highestReserve(partSafetyQuantityDTO.getHighestReserve())
                .deleteFlag(partSafetyQuantityDTO.getDeleteFlag())
                .createUserUid(partSafetyQuantityDTO.getCreateUserUid())
                .createTime(partSafetyQuantityDTO.getCreateTime())
                .build());
    }

    @Transactional
    @Override
    public int remove(String id) {
        return partSafetyQuantityMapper.delete(id);
    }

    @Transactional
    @Override
    public int removeBatch(List<String> ids) {
        int res = 0;
        for (String id : ids) {
            res += partSafetyQuantityMapper.delete(id);
        }
        return res;
    }

    @Override
    public List<PartSafetyQuantityDTO> selectAll() {
        return partSafetyQuantityMapper.selectAll().stream().map(t -> CustomBeanUtils.partSafetyQuantityDO2DTO(t)).collect(Collectors.toList());
    }

//    private PartSafetyQuantityDTO do2dto(PartSafetyQuantityDO t) {
//        PartModelDO partModel = t.getPartModel();
//        PartModelDTO partModelDTO = null;
//        if (partModel != null) {
//            partModelDTO = PartModelServiceImpl.do2dto(partModel);
//        } else {
//            partModelDTO = PartModelDTO.builder().build();
//        }
//        return PartSafetyQuantityDTO.builder()
//                .objuid(t.getObjuid())
//                .stkMainUid(t.getStkMainUid())
//                .partModelUid(t.getPartModelUid())
//                .factoryCode(t.getFactoryCode())
//                .warningQuantity(t.getWarningQuantity())
//                .remark(t.getRemark())
//                .modifyUserUid(t.getModifyUserUid())
//                .modifyTime(t.getModifyTime())
//                .lowestReserve(t.getLowestReserve())
//                .highestReserve(t.getHighestReserve())
//                .deleteFlag(t.getDeleteFlag())
//                .createUserUid(t.getCreateUserUid())
//                .createTime(t.getCreateTime())
//                .partModel(partModelDTO)
//                .build();
//    }

    @Override
    public PageData<List<PartSafetyQuantityDTO>> selectAll(Integer limit, Integer curr) {
        Page<PartSafetyQuantityDO> page = new Page<>(curr, limit);
        List<PartSafetyQuantityDTO> partSafetyQuantityDTOs = partSafetyQuantityMapper.selectPageAll(page).stream().map(t -> CustomBeanUtils.partSafetyQuantityDO2DTO(t)).collect(Collectors.toList());
        return new PageData<>((int) page.getTotal(), partSafetyQuantityDTOs);
    }

    @Override
    public Optional<PartSafetyQuantityDTO> selectOneById(String id) {
        PartSafetyQuantityDO partSafetyQuantityDO = partSafetyQuantityMapper.selectOneById(id);
        if (partSafetyQuantityDO != null) {
            return Optional.of(CustomBeanUtils.partSafetyQuantityDO2DTO(partSafetyQuantityDO));
        }
        return Optional.empty();
    }

    @Override
    public List<PartSafetyQuantityDTO> selectList(PartSafetyQuantityDTO partSafetyQuantityDTO) {
        PartSafetyQuantityDO partSafetyQuantityDO = buildCondition(partSafetyQuantityDTO);
        return partSafetyQuantityMapper.selectList(partSafetyQuantityDO).stream().map(t -> CustomBeanUtils.partSafetyQuantityDO2DTO(t)).collect(Collectors.toList());
    }

    private PartSafetyQuantityDO buildCondition(PartSafetyQuantityDTO partSafetyQuantityDTO) {
        return PartSafetyQuantityDO.builder()
                .objuid(partSafetyQuantityDTO.getObjuid())
                .stkMainUid(partSafetyQuantityDTO.getStkMainUid())
                .partModelUid(partSafetyQuantityDTO.getPartModelUid())
                .factoryCode(partSafetyQuantityDTO.getFactoryCode())
                .warningQuantity(partSafetyQuantityDTO.getWarningQuantity())
                .remark(partSafetyQuantityDTO.getRemark())
                .modifyUserUid(partSafetyQuantityDTO.getModifyUserUid())
                .modifyTime(partSafetyQuantityDTO.getModifyTime())
                .lowestReserve(partSafetyQuantityDTO.getLowestReserve())
                .highestReserve(partSafetyQuantityDTO.getHighestReserve())
                .deleteFlag(partSafetyQuantityDTO.getDeleteFlag())
                .createUserUid(partSafetyQuantityDTO.getCreateUserUid())
                .createTime(partSafetyQuantityDTO.getCreateTime())
                .build();
    }

    @Override
    public PageData<List<PartSafetyQuantityDTO>> selectList(PartSafetyQuantityDTO partSafetyQuantityDTO, Integer limit, Integer curr) {
        Page<PartSafetyQuantityDO> page = new Page<>(curr, limit);
        PartSafetyQuantityDO partSafetyQuantityDO = buildCondition(partSafetyQuantityDTO);
        List<PartSafetyQuantityDTO> partSafetyQuantityDTOs = partSafetyQuantityMapper.selectPageList(partSafetyQuantityDO, page).stream().map(t -> CustomBeanUtils.partSafetyQuantityDO2DTO(t)).collect(Collectors.toList());
        return new PageData<>((int) page.getTotal(), partSafetyQuantityDTOs);
    }
}
